package com.dev.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 请求乱码统一处理
 *  tomcat8及以上版本 POST请求乱码问题
 *  tomecat7以下版本 POST/GET请求乱码问题
 */
@WebFilter("/**")
public class AEncodingFilter implements Filter {
    
    public AEncodingFilter(){
    
    }
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    
    }
    
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 基于HTTP
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        
        // 处理请求乱码问题
        request.setCharacterEncoding("UTF-8");
        
        // 放行
        filterChain.doFilter(request, response);
        
    }
    
    @Override
    public void destroy() {
    
    }
}
